package com.ecoolex.chinadutyfree.retailer.core.manager;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsProductBuyLimitDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderDetail;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProductBuyLimit;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProductBuyLimit.Column;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProductBuyLimitExample;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProductBuyLimitExample.Criteria;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsShoppingCart;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DirectMailGoodsProductBuyLimitManager {
	
    @Autowired
    private DirectMailGoodsProductBuyLimitDAO dao;
    
    @Autowired
    private DirectMailGoodsOrderDetailManager orderDetailManager;
    
    /**
     * 限购校验
     * @param cart
     */
    public void check(DirectMailGoodsShoppingCart cart) {
    	DirectMailGoodsProductBuyLimit limit = getByProductId(cart.getDirectMailProductId());
    	//此产品有限购
    	if(Check.notNull(limit)) {
    		BizAssert.isTrue(Check.isLessThanEqual(cart.getBuyNumber(), limit.getSingleNumber()),ResultCode.VERIFY.build(10), "商品超过单笔限购数量");
    		List<DirectMailGoodsOrderDetail> orderDetailList = orderDetailManager.listByDirectMailGoodsProductId(cart.getDirectMailProductId(),cart.getUserId(), limit.getStartTime(), limit.getEndTime());
    		if(Check.notNullOrEmpty(orderDetailList)) {
    			Integer buyNumber = 0;
        		for (DirectMailGoodsOrderDetail directMailGoodsOrderDetail : orderDetailList) {
    				buyNumber += directMailGoodsOrderDetail.getBuyNumber();
    			}
        		BizAssert.isTrue(Check.isLessThanEqual(buyNumber+cart.getBuyNumber(), limit.getNumber()),ResultCode.VERIFY.build(11), "商品超过限购数量");
    		}
    	}
    }
    
    public DirectMailGoodsProductBuyLimit add(DirectMailGoodsProductBuyLimit record,String createUser) {
    	DirectMailGoodsProductBuyLimit limit = getByProductId(record.getDirectMailProductId());
    	BizAssert.isTrue(Check.isNull(limit),ResultCode.VERIFY.build(10), "此产品已经存在限购策略");
    	record.setDeleted(DirectMailGoodsProductBuyLimit.NOT_DELETED);
    	record.setCreateUser(createUser);
    	record.setCreateTime(Date.from(Instant.now()));
    	record.setUpdateUser(createUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.insert(record);
    	return record;
    }
    
    public DirectMailGoodsProductBuyLimit edit(DirectMailGoodsProductBuyLimit record,String updateUser) {
    	record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.updateUser,Column.updateTime));
    	return record;
    }
    
    public void delete(DirectMailGoodsProductBuyLimit record,String updateUser) {
    	record.setDeleted(DirectMailGoodsProductBuyLimit.IS_DELETED);
    	record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record,Column.deleted,Column.updateUser,Column.updateTime);
    }
    
    public DirectMailGoodsProductBuyLimit get(Integer id) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(id, DirectMailGoodsProductBuyLimit.NOT_DELETED);
    }
    
    public DirectMailGoodsProductBuyLimit getByProductId(Integer directMailProductId) {
    	DirectMailGoodsProductBuyLimitExample example = new DirectMailGoodsProductBuyLimitExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andDirectMailProductIdEqualTo(directMailProductId).andDeletedEqualTo(DirectMailGoodsProductBuyLimit.NOT_DELETED);
    	return dao.selectOneByExample(example);
    }
}